package com.maixin.code.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 提供DATE的转换类
 * @author Administrator
 *
 */
public class DateUtils {
	//保留秒后6位
	private static final String ssStr = ".000000";
	/**
	 * 当前Date增加或减少month个月
	 * @param date 当前时间
	 * @param month 增加或减少month个月
	 * @return
	 */
	public static Date dateAddMonth(Date date ,int month){
		 // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化对象
		  Calendar calendar = Calendar.getInstance();//日历对象
		  calendar.setTime(date);//设置当前日期
		  calendar.add(Calendar.MONTH, month);//月份
		// System.out.println(sdf.format(calendar.getTime()));//输出格式化的日期
		  return calendar.getTime();
	}
	
	
	/**
	 * 将传递的日期转换成yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String date2Str(Date date){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	public static Date str2Date(String str){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		Date ddate;
		try {
			ddate=sdf.parse(str);
			return ddate;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return null;
	}
	
	
	public static String date2Str3(Date date){
		if(null == date){
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	/**
	 * 将传递的日期转换成yyyyMMddHHmmss
	 * @param date
	 * @return
	 */
	public static String date2Str2(Date date){
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date);
	}
	
	public static void main(String[] args) {
//		System.err.println(date2Str2(new Date()));
//		System.err.println(str2DateStr("2011-11-11.9.34. 36. 0"));
//		System.err.println(str2Timestamp2("2011-09-19"));
//		str2Date("19-9月 -11 18.39.18.000000 上午");
		dateAddMonth(new Date(),-2);
//		String s = "2011-9-19.19.59. 2. 0";
//		String ss  = "2011-10-01 20:21:52";
//		System.err.println(str2Timestamp(ss));
	}
	/**
	 * 将oracle数据库中TimeStamp格式的数据：2011-9-23.9.34. 36. 0
	 * 查询出来后，直接获得年月日：2011-09-23
	 * @param str
	 * @return
	 */
	public static String str2DateStr(String str){
		if("".equals(str)||null == str){
			return null;
		}else{
			String date = str.split("\\.")[0];
			String [] dateArr = date.split("-");
			
			if(Integer.parseInt(dateArr[1])<10){
				dateArr[1] = "0"+Integer.parseInt(dateArr[1]);
			}
			if(Integer.parseInt(dateArr[2])<10){
				dateArr[2] = "0"+Integer.parseInt(dateArr[2]);
			}
			String time = dateArr[0]+"-"+dateArr[1]+"-"+dateArr[2];
			return time;
		}
		//return null;
	}
	/**
	 * 将传递的
	 * 2011-09-19
	 * 转成
	 * 19-9月 -11 18.39.18.000000 上午
	 * @param dateStr
	 * @return
	 */
	public static String str2Timestamp2(String dateStr){
		if("".equals(dateStr)|| null == dateStr){
			return "";
		}else{
			
			String[] dateArr = dateStr.split("-");
			StringBuffer sb = new StringBuffer();
			sb.append(dateArr[2]).append("-").
				append(Integer.parseInt(dateArr[1])).append("月 -").
				append(dateArr[0].subSequence(2, 4));
			return sb.toString();
		}
	}
	
	/**
	 * 将传递的
	 * 2011-09-19 18:39:09
	 * 转成
	 * 19-9月 -11 18.39.18.000000 上午
	 * @param dateStr
	 * @return
	 */
	public static String str2Timestamp(String dateStr){
		if("".equals(dateStr)|| null == dateStr){
			return "";
		}else{
			//dateStr = dateStr.replaceAll("\\. 0", "");
			dateStr = dateStr.replaceAll("\\. ", ".");
			dateStr = dateStr.replace(".", " ").replace(":", " ");
			String[] strArr = dateStr.split(" ");
			String date = strArr[0];
			String hh = strArr[1];
			String mm = strArr[2];
			String ss = strArr[3];
			String[] dateArr = date.split("-");
			
			String PM_AM = "";
			if(Integer.parseInt(hh) == 0){
				hh = "12";
				PM_AM ="下午";
			}else{
				if(Integer.parseInt(hh)<=12){
					
					PM_AM ="上午";
				}else{
					hh = (Integer.parseInt(hh)-12)+"";
					PM_AM ="下午";
				}
			}
			StringBuffer sb = new StringBuffer();
			sb.append(dateArr[2]).append("-").
				append(Integer.parseInt(dateArr[1])).append("月 -").
				append(dateArr[0].subSequence(2, 4)).append(" ").
				append(hh).append(".").
				append(mm).append(".").
				append(ss).append(ssStr).
				append(" ").append(PM_AM);
			return sb.toString();
		}
	}
	/**
	 * 比较传递的2个时间类型的数据大小
	 * @param userTime
	 * @param standardTime
	 * @return
	 */
	public static int compareDate(String userTime,String standardTime){
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c1=Calendar.getInstance();
		Calendar c2=Calendar.getInstance();
		try{
			c1.setTime(df.parse(userTime));
			c2.setTime(df.parse(standardTime));
		}catch(Exception e){
			e.printStackTrace();
		}
		return c1.compareTo(c2);
	}
}
